/**
 * 版权：山西书海传媒科技有限责任公司
 * 作者：吕俊雄
 * 更新时间：2019年3月29号
 */
Page({
  data:{
    // 当前选择的导航字母
    selected: 0,
    // 选择字母视图滚动的位置id
    scrollIntoView: 'A',
    // 导航字母
    letters: ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'],
    groups: []
  },
  onLoad:function(options){
    const res = wx.getSystemInfoSync(),
          letters = this.data.letters;
    // 设备信息
    this.setData({
      windowHeight: res.windowHeight,
      windowWidth: res.windowWidth,
      pixelRatio: res.pixelRatio
    });
    // 第一个字母距离顶部高度，css中定义nav高度为94%，所以 *0.94
    const navHeight = this.data.windowHeight * 0.94, // 
          eachLetterHeight = navHeight / 26,
          comTop = (this.data.windowHeight - navHeight) / 2,
          temp = [];
    this.setData({
      eachLetterHeight: eachLetterHeight
    });
    // 求各字母距离设备左上角所处位置
    for(let i = 0, len = letters.length; i < len; i++) {
      const x = this.data.windowWidth - (10 + 50) / this.data.pixelRatio,
            y = comTop + (i * eachLetterHeight);
      temp.push([x, y]);
    }
    this.setData({
      lettersPosition: temp
    })
    //拿code换accesstoken开始
    var that = this;
    wx.request({
      url: 'https://wxapp.shuhai.cc/hh/gain_Accesstoken.action',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        //code: wx.getStorageSync("code_res"),
        code: 'c11298b97603fd17804748015d263349'
      },
      success: function (res) {
        if (res.data.status == "success"){
          console.log("获取accesstoken成功");
          //拿accesstoken获取用户信息的partyId参数开始
          wx.request({
            url: 'https://wxapp.shuhai.cc/hh/gain_TuantiId.action',
            method: 'post',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              access_token: res.data.data.result,
            },
            success: function (res) {
              console.log(res.data);
              if (res.data.status == "success") {
                console.log("获取团体id成功");
                var tuanti_id = res.data.data.result;//团体id
                wx.setStorageSync('tuanti_id_res', tuanti_id);
              } else if (res.data.status == "fail") {
                console.log("获取团体id失败");
              }
            }
          });
    //拿accesstoken获取用户信息的partyId参数结束

        } else if (res.data.status == "fail"){
          console.log("获取accesstoken失败");
        }
      }
    });
    //拿code换accesstoken结束

    wx.request({
      url: 'https://wxapp.shuhai.cc/hh/gain_Address_list.action',
      method: 'post',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        //parayId: wx.getStorageSync("tuanti_id_res"),
        parayId: '374b8c05697be12201698041584d0005',
      },
      success: function (res) {
        console.log(res.data);
				if(res.data.status == "success"){
					var aa = res.data.data;
					that.setData({
					  groups: aa
					})
				}else if(res.data.status == "fail"){
					wx.showToast({
					  title: '网络加载慢，请重新进入',
					  icon: 'none',
					  duration: 2000
					})
					return
				}
      }
    });
  },
  tabLetter(e) {
    const index = e.currentTarget.dataset.index;
		console.log(index + "===========index");
    this.setData({
      selected: index,
      scrollIntoView: index
    })

    this.cleanAcitvedStatus();
  },
  // 清除字母选中状态
  cleanAcitvedStatus() {
    setTimeout(() => {
      this.setData({
          selected: 0
      })
    }, 500);
  },
  touchmove:function(e) {
    const x = e.touches[0].clientX,
          y = e.touches[0].clientY,
          lettersPosition = this.data.lettersPosition,
          eachLetterHeight = this.data.eachLetterHeight,
          letters = this.data.letters;
    console.log(y);
    // 判断触摸点是否在字母导航栏上
		console.log("x======>"+x);
		console.log("lettersPosition[0][0]======>"+lettersPosition[0][0]);
    if(x >= lettersPosition[0][0]) {
      for(let i = 0, len = lettersPosition.length; i < len; i++) {
        // 判断落在哪个字母区域，取出对应字母所在数组的索引，根据索引更新selected及scroll-into-view的值
        const _y = lettersPosition[i][1], // 单个字母所处高度
              __y = _y + eachLetterHeight; // 单个字母最大高度取值范围
        if(y >= _y && y <= __y) {
           this.setData({
            selected: letters[i],
            scrollIntoView: letters[i]
          });
          break;
        }
      }
    }
  },
  touchend(e) {
    this.cleanAcitvedStatus();
  },
	goNameId(e){
		var id = e.currentTarget.dataset.id;
		var name = e.currentTarget.dataset.name;
		wx.navigateTo({
		  url: '../tongxunId/tongxunId?id_s='+id+"&name_s="+name,
		})
	}
})